home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-12-13 | 4.3 KB | 164 lines | [TEXT/CCL2] |
- ;;; -*- pAckAge: CL-USER -*-
- ;;;
- ;;; ExAmple for the MCL <-> Code WArrior interfAce
- ;;;
-
-
- (in-pAckAge "CL-USER")
- (use-pAckAge "CODE-WARRIOR")
-
-
- (defvAr *resource-file*
- nil)
-
-
- (defcmodule exAmple
- ;;
- ;;
- :resource-file
- (or *resource-file*
- (setq *resource-file*
- (progn
- (messAge-diAlog "PleAse select the 'exAmple.res' file.")
- (choose-file-diAlog))))
- :vAriAbles
- (my-long
- my-double)
- :functions
- ((test-fixnum (:lisp) :d0)
- (test-chArActer (:lisp) :d0)
- ;;
- ;; If you wAnt your Arguments to hAve more meAningfull nAmes
- ;; you cAn use this AlternAte syntAx for the Arguments.
- ;;
- (test-list ((list :lisp)) :novAlue)
- (test-struct (:lisp) :novAlue)
- (test-vector (:lisp) :novAlue)
- (test-string (:lisp) :novAlue)
- ;;
- ;; The ThinkC type thAt corresponds to MCL's floAting point
- ;; is short double. I provided A wAy to pAss ThinkC doubles
- ;; between C And MCL becAuse this is the type of the 6888*.
- ;; So if you bAdly need efficiency you cAn use this.
- ;;
- (test-short-double (:lisp) :novalue)
- ;;(test-double (:mac) :novalue)
- (test-c-structures (:mac) :novalue)
- (test-a0 (:mac) :a0)
- ;;
- ;; If you want ThinkC to make call backs to Lisp you have to be
- ;; carefull because a callback can give rise to a GC which can
- ;; invalidate the pointers that C has in the Lisp world.
- ;; Gary Byers pointed out to me that even if you dont make any
- ;; call backs to Lisp, a request to the macintosh memory manager
- ;; can possibly result in triggering a MCL garbage collect!
- ;;
- ;; Your C code can make callbacks to Lisp because it only gets
- ;; pointers to the actual arguments, those pointers being updated
- ;; automatically in case of a garbage collection.
- ;;
- (test-callback (:lisp :mac) :d0)
- (test-globals () :d0)
- (test-multi-segment () :d0)
- (test-traps () :a0)))
-
-
- ;;
- ;; LOAD-CMODULE reads the main segment of your code resource in memory
- ;; and put the address in the symbol naming your C module (here EXAMPLE).
- ;;
- ;; LOAD-CMODULE will also link the variables and functions you defined in
- ;; your C module with their correct address in memory. The symbol value of
- ;; the variables will containt the address of their C counterparts and the
- ;; symbol value of the functions will contain their entry point address.
- ;;
- ;; It is important to understand that MCL doesnt conserve the Mac heap
- ;; accross a SAVE-APPLICATION. This implies that in order for your C modules
- ;; to be restored properly, the resource file must still be present even
- ;; after a SAVE-APPLICATION.
- ;;
- (load-cmodule 'example)
-
- ;;
- ;; CLOSE-CMODULE closes the resource file associated with your C module.
- ;; It is very important if your C module is multi-segmented, that you do
- ;; not close it before you are through using it, as a call to an unloaded
- ;; segment will force the segment loader to load it from your resource file.
- ;;
- (close-cmodule 'example)
-
-
- ;;
- ;; A typical debugging cycle for your C code will be the following:
- ;; You evaluate your DEFCMODULE form only once.
- ;; After that, you load your C module with LOAD-CMODULE, test it,
- ;; close it with CLOSE-CMODULE, go back to ThinkC, make changes to your code,
- ;; rebuild the code resource, and the cycles repeats itself.
- ;;
-
-
- #|
- (test-fixnum 10)
-
- (test-character #\a)
- (test-character #\b)
- (test-character #\h)
-
- (setq l '(1 6 7 8 9))
- (test-list l)
- l
-
- (defstruct foo a b c)
- (setq s (make-foo :a 10 :b 20 :c 30))
- (test-struct s)
- s
-
- (setq a (make-array 5 :initial-contents '(1 2 3 4 5)))
- (test-vector a)
- a
-
- (setq s "hello how are yah")
- (test-string s)
- s
-
- (setq a 2.3)
- (test-short-double a)
- a
-
- ;;(setq a (%make-double 2.3))
- ;;(test-double a)
- ;;(%get-double a)
-
- (defrecord :myrecord
- (a :long)
- (b :long)
- (c :long))
- (setq a (make-record :myrecord :a 3 :b 4 :c 8))
- (test-c-structures a)
- (print-record a :myrecord)
-
- (setq p (#_newPtr 10))
- (test-a0 p)
-
- (defccallable gc (:void) (gc))
- (test-callback '(2 3 4) gc)
-
- (test-globals)
-
- ;;
- ;; If you inadvertently evaluated the CLOSE-CMODULE form
- ;; in the middle of this file, it is not to late to reload your
- ;; module with the LOAD-CMODULE form, before it crashes with an
- ;; error of type 15!
- ;;
- (test-multi-segment)
-
- (test-traps)
-
- (%get-long my-long)
- ;;(%get-double my-double)
-
- (setf (%get-long my-long) 7)
- (test-globals)
- |#
-